package com.itheima.controller;

import com.itheima.anno.OperateLog;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;

    //分页查询
    @GetMapping("/students")
    public Result findAll(StudentQueryParam s){
        log.info("查询条件{}",s );
        PageResult pageBean = studentService.findAll(s);
        return Result.success(pageBean);
    }

    @OperateLog
    //删除学员
    @DeleteMapping("/students/{ids}")
    public Result deleteById(@PathVariable("ids") List<Integer> ids){
        studentService.deleteById(ids);
        return Result.success();
    }

    @OperateLog
    //添加学员
    @PostMapping("/students")
    public Result insertStudent(@RequestBody Student student){
        studentService.insert(student);
        return Result.success();
    }

    //根据id查询学员
    @GetMapping("/students/{id}")
    public Result findById(@PathVariable("id") Integer id){
        Student student = studentService.findById(id);
        return Result.success(student);
    }

    @OperateLog
    //修改学员数据
    @PutMapping("/students")
    public Result update(@RequestBody Student student){
        log.info("更新数据：{}", student);
        studentService.update(student);
        return Result.success();
    }

    @OperateLog
    //违纪处理
    @PutMapping("/students/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id,  @PathVariable String score){
        studentService.updateViolation(id, score);
        return Result.success();
    }
}
